const app = getApp();
const { htmlDecode} = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bigSearchShow:false,
    bigSearchType:"",
    searchDataList:[],
    dutyDept: '',
    fileUrl: app.globalData.fileUrl,
    risks: [],
    areaList: [],
    areaIndex: '',
    sapId: null,
    myName: null,
    proposeName: '',
    dealFlag: 1,
    nodeArray: ['申请退回', '指派责任部门', '指派处理', '隐患排除中', '整改后审核', '闭环'],
    flowTypeArray: ['指派责任部门','指派整改人','整改隐患','整改后审核','延期审核','提出隐患','申请延期'],
    nodeIndex: '',
    baseUrl: app.globalData.baseUrl,
    x:10, //添加的拖拽X轴
    y:400 //添加的拖拽Y轴
  },
  cancelSelection(){
    this.setData({
      areaIndex: ''
    })
  },
  setProposeName(e){
    this.setData({
      proposeName: e.detail.value
    })
  },
  doBigSearchShow(e) {
    console.log('e', e);
    console.log(this.data.deptList.length);
    const tempArray = [];
    const typeMethods = {
      dept: () => {
        if (this.data.deptList.length == 0) {
          wx.showToast({
            title: '暂无部门',
            icon: 'none'
          });
          return;
        } else {
          this.data.deptList.forEach(item => {
            tempArray.push({
              name: item
            });
          });
        }
      }
    }
    typeMethods[e.currentTarget.dataset.type]();

    this.setData({
      bigSearchType: e.currentTarget.dataset.type,
      bigSearchShow: true,
      searchDataList: tempArray
    })
  },
  searchSelect(e) {
    this.setData({
      dutyDept : e.detail.name||""
    });
  },
  getAllDept: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/deptInfo/wx/getAllDept',
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有部门信息:', res.data);
        var data = res.data;
        that.setData({
          deptList: data.data
        })
      }
    });
  },

  areaChange: function (e) {
    this.setData({
      areaIndex: e.detail.value
    })
  },

  addTouchStart:function(e) {
    
    this.startX = e.touches[0].pageX;
    this.startY = e.touches[0].pageY;
  },
  addTouchMove:function(e) {
    const moveX = e.touches[0].pageX;
    const moveY = e.touches[0].pageY;
    const dx = moveX - this.startX;
    const dy = moveY - this.startY;
 
    this.setData({
      x: this.data.x + dx,
      y: this.data.y + dy
    });
 
    this.startX = moveX;
    this.startY = moveY;
  },

  initSapId: function(){
    var userInfo = wx.getStorageSync('userinfo');
    console.log('userInfo',userInfo);
    this.setData({
      sapId: userInfo.siocSapNo,
      myName: userInfo.siocCnName
    })
  },
  nodeChange(e){
    this.setData({
      nodeIndex: e.detail.value
    });
  },
  nodeCancel(){
    this.setData({
      nodeIndex: ''
    });
  },
  loadData(){
    if(this.data.dealFlag == 2 || this.data.dealFlag == 3){
      this.getAuditRisk(this.data.dealFlag);
    }else{
      this.getMyRisk();
    }
  },
  getMyRisk: function () {
    var that = this;
    var area = that.data.areaList[that.data.areaIndex] == undefined ? '' : that.data.areaList[that.data.areaIndex];
    wx.request({
      url: that.data.baseUrl+'/rmStaff/wx/getMyRisk?sapId='+that.data.sapId+'&node='+that.data.nodeIndex+'&area='+area+'&dutyDept='+that.data.dutyDept,
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有我提交的隐患:', res.data);
        var data=res.data;

        data.rows.forEach(item=>{
          item.content = htmlDecode(item.content)
          item.location = htmlDecode(item.location)
          item.suggest = htmlDecode(item.suggest)
        })

        that.setData({
          riskList: data.rows,
          dealFlag: 1
        })
      }
    });
  },
  getMyAuditRisk: function (e) {
    
    var tempDealFlag = e.currentTarget.dataset.operation;
    this.getAuditRisk(tempDealFlag);
  },

  getAuditRisk(dealFlag){
    var that = this;
    var area = that.data.areaList[that.data.areaIndex] == undefined ? '' : that.data.areaList[that.data.areaIndex];
    wx.request({
      url: that.data.baseUrl+'/rmStaff/wx/getMyAuditRisk?name='+that.data.myName+'&dealFlag='+dealFlag+'&userSapId='+that.data.sapId+'&node='+that.data.nodeIndex+'&proposeName='+that.data.proposeName+'&area='+area,
      method: 'GET', 
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有我审核的隐患:', res.data);
        var data=res.data;
        that.setData({
          riskList: data.rows,
          dealFlag: dealFlag
        })
      }
    });
  },
  goAdd(){
    wx.navigateTo({
      url: '/pages/risk-staff/risk-staff',
    })
  },
  getAllArea: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/areaInfo/wx/getAllArea',
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有区域信息:', res.data);
        var data = res.data;
        that.setData({
          areaList: data.data
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initSapId();
    this.getAuditRisk(2);
    this.getAllArea();
    this.getAllDept();
    this.startX = 0;
    this.startY = 0;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})